package com.kfm.am;

import java.util.Comparator;
import java.util.Vector;

public class Test04 {

    public static void main(String[] args) {
        // 创建一个Vector集合，用于存储一组学生的成绩。
        // 编写一个函数，接受一个分数范围，并返回在该范围内的学生数量。
        Vector vector = new Vector();
        vector.add(89.0);
        vector.add(98.0);
        vector.add(86.0);
        vector.add(67.0);
        vector.add(60.5);
        vector.add(79.8);
        vector.sort(new Test04Comparator());
        sort(vector, new Test04Comparator());
        System.out.println(countRange(vector, 97, 68));
    }

    public static int countRange(Vector vector, int big, int small) {
        if (big < small) {
            return -1;
        }
        int count = 0;
        for (Object object : vector) {
            double d = (double) object;
            if (d >= small && d <= big) {
                count++;
            }
        }
        return count;
    }

    public static void sort(Vector vector, Comparator c) {
        for (int i = 0; i < vector.size() - 1; i++) {
            for (int j = 0; j < vector.size() - i - 1; j++) {
//                double d1 = (double)vector.get(j);
//                double d2 = (double)vector.get(j + 1);
//                int compare = ;
                if (c.compare(vector.get(j), vector.get(j + 1)) > 0) { // d1 > d2
                    // 交换位置
                    Object temp = vector.set(j, vector.get(j + 1));
                    vector.set(j + 1, temp);
                }
            }
        }
    }
}

class Test04Comparator implements Comparator {

    @Override
    public int compare(Object o1, Object o2) {
        double d1 = (double) o1;
        double d2 = (double) o2;
        return (int) (d1 - d2);
    }
}
